﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using MyList_Enumerator;

namespace MyList_Enumerator
{
    class MyListTEnum_insert<T> : IEnumerator<T> where T : IComparable
    {
        private T current;
        private int position;
        private int positionStart;
        private int positionEnd;
        private int index;
        private T[] elements;

        public MyListTEnum_insert(T[] elements)
        {
            // TODO: Complete member initialization
            this.elements = elements;
            position = -1;
            positionStart = 0;
            positionEnd = elements.Length;
            index = 0;
        }

       
        public T Current
        {
            get { return current; }
        }

        public void Dispose()
        {
        }

        object IEnumerator.Current
        {
            get
            {
                try
                {
                    current = elements[position];
                    return current;
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }

        public bool MoveNext()
        {
            if (index++ % 2 == 0)
            {
                position = positionStart++;
            }
            else
            {
                position = positionEnd--;
            }
            
            return (positionEnd >= positionStart);
        }

        public void Reset()
        {
            position = -1;
            positionStart = -1;
            positionEnd = elements.Length;
            index = 0;
        }
    }
}
